#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: 
# *predefine_variable.sh*
# Description: 
#   man bash, find  "Shell Variables"
#  	These are standard variables defined by the bash shell itself, they needn't be defined in the script.

#   $LINENO 目前的行號

#   % source xxx.sh
#   相當於用.這個符號來帶入
#   % . xxx.sh
#   
# 	$# The number of arguments supplied to a script
# 	$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
# 	$? Exit status of previous command
# 	$$ PID of this shell's process
#	$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
#	$! The process number of the last background command.
#	$0 The name of the command being executed. For shell scripts, this is the path with which it was invoked.
#	$n These variables correspond to the arguments with which a script was invoked.

#	 #	註解符號：這個最常被使用在 script 當中，視為說明！在後的資料均不執行
#	 \	跳脫符號：將『特殊字元或萬用字元』還原成一般字元
#	 |	管線 (pipe)：分隔兩個管線命令的界定(後兩節介紹)；
#	 ;	連續指令下達分隔符號：連續性命令的界定 (注意！與管線命令並不相同)
#	 ~	使用者的家目錄
#	 $	取用變數前置字元：亦即是變數之前需要加的變數取代值
#	 &	工作控制 (job control)：將指令變成背景下工作
#	 !	邏輯運算意義上的『非』 not 的意思！
#	 /	目錄符號：路徑分隔的符號
#	 >, >>	資料流重導向：輸出導向，分別是『取代』與『累加』
#	 <, <<	資料流重導向：輸入導向 (這兩個留待下節介紹)
#    n>&m	means redirect FD n to the same places as FD m. Eg, 2>&1 means send STDERR to the same place that STDOUT is going to.
#	 ' '	單引號，不具有變數置換的功能
#	 " "	具有變數置換的功能！
#	 ` `	兩個『 ` 』中間為可以先執行的指令，亦可使用 $( )
#	 ( )	在中間為子 shell 的起始與結束
#	 { }	在中間為命令區塊的組合！
#   
#   ---Special Shell Characters---
#	Character	  Explanation
#   $			  Indicates the beginning of a shell variable name
#   |			  Pipes standard output to next command
#   #			  Starts a comment
#   &			  Executes a process in the background
#   ?			  Matches one character
#   *			  Matches one or more characters
#   >			  Output redirection operator
#   <			  Input redirection operator
#   `			  Command substitution (the backquote or backtickthe key above the Tab key on most keyboards)
#   >>			  Output redirection operator (to append to a file)
#   <<			  Wait until following end-of-input string (HERE operator)
#   [ ]			  Range of characters
#   [a-z]		  All characters a through z
#   [a,z]		  or [az] Characters a or z
#   Space		  Delimiter between two words
#
# Edit History: 
#   2010-10-30    File created.
#========================================================================
#argc
#argv
#/path/to/scriptname  opt1  opt2  opt3  opt4  ...
#       $0             $1    $2    $3    $4   ...

# uname指令會列出OS的版本或CPU的種類等資訊

#get_systype() {
MACHINE=`uname -m`
PROCESSOR=`uname -p`
OS=`uname -s`
#}                       

echo "            argv: $0 $1 $2 $3 $4"
echo "            argc: $#"
echo "       目前第x行: $LINENO"
echo "          家目錄: $HOME"
echo "      使用者名稱: $USER"
echo "      使用者名稱: $USERNAME"
echo "        使用者ID: $UID"
echo "    目前所在目錄: $PWD"
echo "        作業系統: $OSTYPE"
echo "        Bash版本: $BASH_VERSION"
echo "Machine hardware: $MACHINE"
echo "  Processor type: $PROCESSOR"
echo "perating system:: $OS"
#Increments by one each time an instance of bash is started.
#This variable is useful for determining whether the built-in exit command ends the current session.
echo "$SHLVL"
#REPLY Expands to the last input line read by the read built-in command when it is given no arguments. 
#This variable is not available in sh.
#read 
#echo "最後一次read的值: $REPLY"
#RANDOM Generates a random integer between 0 and 32,767 each time it is referenced.
#You can initialize the sequence of random numbers by assigning a value to $RANDOM.
#This variable is not available in sh.
echo "     亂數0~32767: $RANDOM"
#Each time this parameter is referenced, it returns the number of seconds since shell invocation.
echo "      已執行幾秒: $SECONDS"
#Indicates the Internal Field Separator that is used by the parser for word splitting after expansion.
#$IFS is also used to split lines into words with the 'read' built-in command. 
#The default value is the string, " \t\n ", where " " is the space character, \t is the tab character, and \n is the newline character.
#IFS用於特殊符號要加上$
IFS=$'\n'
IFS=,
echo "             IFS: $IFS"
echo "  目前shell的pid: $$"
ls | wc -k | foo
echo "array存放管道中每个命令的exit status: ${PIPESTATUS[*]}" # "0 1 127" - unless you have a "foo" command on your system. "ls" is 0(success), "wc -k" is 1(because "wc -k" fails.), and the non-existent foo causes the "127".
echo "表示程式之後所有位置參數的總數為何:$#"
echo "表示程式之後所有位置參數名稱列表:$*"	
echo "表示程式之後所有位置參數名稱列表:$@"	#與「*」是一樣的意思，但如果在取得其變數值時，使用雙引號將它們包住，那就有所差異了，這部分在稍後的範例中會說明。
#在 Shell 下所執行的程式結束時，會傳回一個整數的編號給 Shell，一般傳回 0 表示程式執行無誤 (true)，若傳回非 0 的值，表示程式執行過程出了錯誤 (false)。
#在 scripts 中，可以使用 exit 指令來退出 script 的執行，並可同時設定 exit value。但如果是在函式裡頭，則需使用 return 指令來結束函式的執行，當然也可同時將 return value 設定上去。
echo "上個程式結束回傳值:$?"	#表示程式的結束狀態傳回值 (exit value 或 return value)。

ping www.google.com.tw > /dev/null&
#$! PID of the most recently started backgroup job
LASTPID=$(echo $!)
# OR
# LASTPID=$!
echo "上一次執行的背景程式的pid:${LASTPID}"
kill -9 ${LASTPID}

function foo
{
	echo "foo參數:$0 $1 $2"
	#IFS 全名為 Internal Field Separator，是 shell 預設所使用的內部欄位分隔符號
	IFS=:
	echo "foo:{"
	echo "參數總數:$#"
	echo "$*" #會有:分隔,且參數"a b"會視為一個參數
	for n in $* ; do
		echo "\$*:$n"
	done

	for n in $@ ; do
		echo "\$@:$n"
	done
	echo "$@"
	echo "}"
}

foo a b c
foo "a b" c


#========================================================================
# 改變LANG的值來執行date
#========================================================================
for LANG in C ja_JP.eucJP ja_JP.UTF-8 en_US.ISO8859-1 fr_FR.ISO8859-1; do
    echo -n "$LANG -- "
    #echo "$LANG -- \c"   # Solaris的情況
    date
done                    
